package com.starblink.store.memanage.adapter

import com.guadou.lib_baselib.view.span.dsl.buildSpannableString
import com.starblink.android.basic.adapter.BaseBindingVH
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.sensorsdata.extension.businessSuffixId
import com.starblink.android.basic.sensorsdata.extension.trackData
import com.starblink.android.basic.sensorsdata.room.entity.SkAntEntity
import com.starblink.android.basic.sensorsdata.spm.GTrackerAssistUtils
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.android.basic.sensorsdata.spm.event.TrackEvent
import com.starblink.android.basic.sensorsdata.spm.impl.SpmTrackHandler
import com.starblink.android.basic.util.FontUtil
import com.starblink.android.basic.util.post.CommonMutateManage
import com.starblink.android.common.R
import com.starblink.basic.ext.load
import com.starblink.basic.util.CommUtils
import com.starblink.rocketreserver.fragment.BrandVOF
import com.starblink.store.databinding.ItemMemanageBrandBinding

class SubBrandCell(
    binding: ItemMemanageBrandBinding,
    val detailListener: (Int, BrandVOF) -> Unit,
    val unFollowListener: (Int, BrandVOF) -> Unit,
) :
    BaseBindingVH<BrandVOF, ItemMemanageBrandBinding>(binding = binding) {

    override fun onBind(merchat: BrandVOF, position: Int) {

        binding.manage.cont.trackData(
            page = SpmPageDef.MeBrandSubListPage,
            element = SpmElementDef.Element_33014,
            params = mapOf(
                "brandName" to "${merchat.name}",
                "index" to position.toString()
            ),
//            businessSuffixID = businessSuffixId("My_MerchantWebVOF", merchat.id)
            businessSuffixID = businessSuffixId(merchat)
        )
        binding.manage.cont.click {
            detailListener.invoke(position, merchat)
        }

        binding.deleteBtn.click {
            merchat.brandId?.let { it1 ->
                CommonMutateManage.subscribeOrNotBrand(
                    itemView.context, merchat.hasSub,
                    it1, brand = merchat,
                    cancelNotCallback = {
                        binding.laySidesLipDelete.quickClose()
                    },
                    resultCallback = {
                        binding.laySidesLipDelete.quickClose()
                        unFollowListener.invoke(position, merchat)
                    }
                )
            }
            SpmTrackHandler.addSingleTrackData(
                SkAntEntity(
                    event = TrackEvent.elementClick.value,
                    spmCnt = GTrackerAssistUtils.fetchElementValue(
                        SpmPageDef.MeBrandSubListPage,
                        SpmElementDef.Element_33087
                    ),
                    params = mapOf(
                        "brandName" to (merchat.name ?: ""),
                        "index" to position.toString(),
                        "result" to "0"
                    )
                )
            )
        }

        binding.manage.tagRed.visibleOrGone(merchat.read == false) {}

        binding.manage.ivBg.load(merchat.brandImg)
        binding.manage.title.text = merchat.name
        binding.manage.ivStore.load(merchat.logo ?: "", merchat.name)
        binding.manage.tvFollowers.text =
            "${merchat.subNums} ${CommUtils.getString(com.starblink.basic.style.R.string.following_2)}"
        binding.manage.ivDiscount.visibleOrGone(merchat.brandDiscount ?: 0 > 0) {}
        binding.manage.div.visibleOrGone(merchat.brandDiscount ?: 0 > 0) {}
        binding.manage.tvDiscount.visibleOrGone(merchat.brandDiscount ?: 0 > 0) {
            setBackgroundResource(R.drawable.tag_store_discount)
            buildSpannableString {
                addText("Up To ") {
                    FontUtil.getTypeface(FontUtil.MONTSERRAT_MEDIUM)?.let {
                        setTypeface(it)
                    }
                }
                addText("${merchat.brandDiscount}%") {
                    setScale(1.2f)
                    FontUtil.getTypeface(FontUtil.MONTSERRAT_BOLD)?.let {
                        setTypeface(it)
                    }
                }
                addText(" Off") {
                    FontUtil.getTypeface(FontUtil.MONTSERRAT_MEDIUM)?.let {
                        setTypeface(it)
                    }
                }
            }
        }
    }
}